package function;

/**
 * @Description TODO
 * @Author li
 * @Date 2022/3/14 9:50 上午
 */
public class MaxValue {
    public static void main(String[] args) {
        int[][] a = new int[][]{
                {1, 3, 1},
                {1, 5, 1},
                {4, 2, 1}
        };
        maxValue(a);
    }

    public static int maxValue(int[][] grid) {
        int m = grid.length, n = grid[0].length;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (i == 0 && j == 0) {
                    continue;
                }
                if (i == 0) {
                    int i1 = grid[i][j - 1];
                    int i2 = grid[i][j];
                    grid[i][j] += i1;
                } else if (j == 0) {
                    int i1 = grid[i - 1][j];
                    int i2 = grid[i][j];
                    grid[i][j] += i1;
                } else {
                    int i1 = grid[i - 1][j];
                    int i2 = grid[i][j - 1];
                    int max = Math.max(i2, i1);
                    grid[i][j] += max;
                }
            }
        }
        return grid[m - 1][n - 1];
    }

}
